package com.wgt.observer.option_status;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by wgt on 2017/6/18.
 */
public class WeatherSubject extends WGTObservable {

    private String content;
    private Integer useStatus;

    public static final int FINE =1;
    public static final int RAIN =2;
    public static final int SNOW =3;

    //天晴需要通知的用户
    private List<WGTObserver> fines;
    //下雨需要通知的用户
    private List<WGTObserver> rains;
    //下雪需要通知的用户
    private List<WGTObserver> snows;

    private int currentWeather = 0; //0 表示没有天气信息 1，天晴 2，下雨 3，下雪

    public WeatherSubject() {
        fines = new ArrayList<>();
        rains = new ArrayList<>();
        snows = new ArrayList<>();
    }

    /**
     * 覆盖父类的
     * @param o
     */
    @Override
    public synchronized void addObserver(WGTObserver o) {
        if (!(o instanceof WeatherObserver)){
            throw new IllegalArgumentException("parameter type is define!");
        }
        WeatherObserver weatherObserver = (WeatherObserver)o;
        for (int status:((WeatherObserver)o).useStatus) {


            if (status == FINE&&!fines.contains(o)) {
                fines.add(o);
            }
            if (status == RAIN&&!rains.contains(o)){
                rains.add(o);
            }
            if (status == SNOW&&!snows.contains(o)){
                snows.add(o);
            }
        }

    }

    @Override
    protected void notifyObservers(Object o) {
        if (!(o instanceof String)){
            throw new IllegalArgumentException("notify observers message type must be String!");
        }
        if (!this.hasChanged()){
            return;
        }

        String content = (String)o;
        if (currentWeather == WeatherSubject.FINE){
            for (WGTObserver observer :fines){
                observer.update(this,content);
            }
        }else if (currentWeather == WeatherSubject.RAIN){
            for (WGTObserver observer : rains){
                observer.update(this,content);
            }
        }else{
            for (WGTObserver observer : snows){
                observer.update(this,content);
            }
        }

    }

    /**
     * 调用该方法获取当前天气信息，并推送
     */
    public void getCurrentWeather(){
        int weather = new Random().nextInt(100);
        String content = null;
        if (weather%2==0){
            this.currentWeather = 2;
            content = "今天天气阴雨天，不适宜出门，出门需要带伞！";
        }else if (weather%3==0) {
            this.currentWeather = 3;
            content = "今天天气寒冷，下午局部会有大雪，注意防寒保暖！";
        }else{
            this.currentWeather = 1;
            content = "今天天气晴朗号，适宜户外运动，踏青是个不错的选择";
        }
        System.out.println("生成随机数是："+currentWeather);
        System.out.println("发送的内容是: "+content);

        this.setChanged();

        this.notifyObservers(content);
    }

    @Override
    protected void notifyObservers() {
        notifyObservers(null);
    }


    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Integer getUseStatus() {
        return useStatus;
    }

    public void setUseStatus(Integer useStatus) {
        this.useStatus = useStatus;
    }
}
